//
//  CLRMProgressView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/31.
//

#import "CLRMProgressView.h"
#import "ASValueTrackingSlider.h"
#import "CLReadController.h"
#import "NSObject+Public.h"
@interface CLRMProgressView() <ASValueTrackingSliderDelegate,ASValueTrackingSliderDataSource>

/// 上一章
@property (nonatomic, strong) UIButton *previousChapter;

/// 进度
@property (nonatomic, strong) ASValueTrackingSlider *slider;

/// 下一章
@property (nonatomic, strong) UIButton *nextChapter;

@end
@implementation CLRMProgressView

- (void)addSubviews {
    [super addSubviews];
    self.backgroundColor = UIColor.clearColor;
    
    // 上一章
    UIButton *previousChapter = [UIButton buttonWithType:UIButtonTypeCustom];
    self.previousChapter = previousChapter;
    previousChapter.titleLabel.font = CL_FONT_SA_14;
    [previousChapter setTitle:@"上一章" forState:UIControlStateNormal];
    [previousChapter setTitleColor:CL_READ_COLOR_MENU_COLOR forState:UIControlStateNormal];
    [previousChapter addTarget:self action:@selector(clickPreviousChapter) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:previousChapter];
    
    // 下一章
    UIButton *nextChapter = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextChapter = nextChapter;
    nextChapter.titleLabel.font = CL_FONT_SA_14;
    [nextChapter setTitle:@"下一章" forState:UIControlStateNormal];
    [nextChapter setTitleColor:CL_READ_COLOR_MENU_COLOR forState:UIControlStateNormal];
    [nextChapter addTarget:self action:@selector(clickNextChapter) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextChapter];
    
    // 进度条
    ASValueTrackingSlider *slider = [[ASValueTrackingSlider alloc]init];
    self.slider = slider;
    slider.delegate = self;
    slider.dataSource = self;
    [slider setThumbImage:[[UIImage imageNamed:@"slider"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    // 设置显示进度保留几位小数 (由于重写了 dataSource 则不用不到该属性了)
    // slider.setMaxFractionDigitsDisplayed(0)
    // 设置气泡背景颜色
    slider.popUpViewColor = CL_READ_COLOR_MAIN;
    // 设置气泡字体颜色
    slider.textColor = CL_READ_COLOR_MENU_COLOR;
    // 设置气泡字体以及字体大小
    slider.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:22];
    // 设置气泡箭头高度
    slider.popUpViewArrowLength = CL_SPACE_SA_5;
    // 设置当前进度颜色
    slider.minimumTrackTintColor = CL_READ_COLOR_MAIN;
    // 设置总进度颜色
    slider.maximumTrackTintColor = CL_READ_COLOR_MENU_COLOR;
    // 设置当前拖拽圆圈颜色
    slider.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:slider];
    [self reloadProgress];
}


/// 刷新阅读进度显示
- (void)reloadProgress {
   
    // 有阅读数据
    CLReadModel *readModel = self.readMenu.vc.readModel;
    
    // 有阅读记录以及章节数据
    if (readModel != nil && (readModel.recordModel.chapterModel != nil)) {
        
        if (CLReadConfigure.sharedInstance.progressType == CLProgressTypeTotal) { // 总进度
            
            self.slider.minimumValue = 0;
            self.slider.maximumValue = 1;
            self.slider.value = [self CL_READ_TOTAL_PROGRESS:readModel recordModel:readModel.recordModel];
            
        }else{ // 分页进度
            
            self.slider.minimumValue = 1;
            self.slider.maximumValue = readModel.recordModel.chapterModel.pageCount;
            self.slider.value = readModel.recordModel.page + 1;
        }
        
    }else{ // 没有则清空
        
        self.slider.minimumValue = 0;
        self.slider.maximumValue = 0;
        self.slider.value = 0;
    }
}

/// 上一章
- (void)clickPreviousChapter {
    if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickPreviousChapter:)]) {
        [self.readMenu.delegate readMenuClickPreviousChapter:self.readMenu];
    }
}

/// 下一章
- (void)clickNextChapter {
    if ([self.readMenu.delegate respondsToSelector:@selector(readMenuClickNextChapter:)]) {
        [self.readMenu.delegate readMenuClickNextChapter:self.readMenu];
    }
}

// MARK: ASValueTrackingSliderDataSource

- (NSString *)slider:(ASValueTrackingSlider *)slider stringForValue:(float)value {
    
    if ([CLReadConfigure sharedInstance].progressType == CLProgressTypeTotal) { // 总进度

        // 如果有需求可显示章节名
        return CL_READ_TOTAL_PROGRESS_STRING(value);

    }else{ // 分页进度

        return @(value).stringValue;
    }
}

// MARK: -- ASValueTrackingSliderDelegate
//- (void)sliderDidHidePopUpView:(ASValueTrackingSlider *)slider;
/// 进度显示将要显示
- (void)sliderWillDisplayPopUpView:(ASValueTrackingSlider *)slider { }

/// 进度显示将要隐藏
- (void)sliderWillHidePopUpView:(ASValueTrackingSlider *)slider {

    if ([CLReadConfigure sharedInstance].progressType == CLProgressTypeTotal) { // 总进度
        
        // 有阅读数据
        CLReadModel *readModel = self.readMenu.vc.readModel;

        // 有阅读记录以及章节数据
        if (readModel != nil && (readModel.recordModel.chapterModel != nil)) {

            // 总章节个数
            NSInteger count = (readModel.chapterListModels.count - 1);

            // 获得当前进度的章节索引
            NSInteger index = (NSInteger)(count * slider.value);

            // 获得章节列表模型
            CLReadChapterListModel *chapterListModel = readModel.chapterListModels[index];

            // 页码
            NSInteger toPage = (index == count) ? CL_READ_LAST_PAGE : 0;

            // 传递
            if ([self.readMenu.delegate respondsToSelector:@selector(readMenuDraggingProgress:toChapterid:toPage:)]) {
                [self.readMenu.delegate readMenuDraggingProgress:self.readMenu toChapterid:chapterListModel.chapterid toPage:toPage];
            }
        }

    }else{ // 分页进度

        if ([self.readMenu.delegate respondsToSelector:@selector(readMenuDraggingProgress:toPage:)]) {
            [self.readMenu.delegate readMenuDraggingProgress:self.readMenu toPage:self.slider.value - 1];
        }
      
    }
}

- (void)layoutSubviews {

    [super layoutSubviews];

    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    CGFloat buttonW = CL_SPACE_SA_55;

    // 上一章
    self.previousChapter.frame = CGRectMake(CL_SPACE_SA_5, 0, buttonW, h);

    // 下一章
    self.nextChapter.frame = CGRectMake(w - buttonW - CL_SPACE_SA_5, 0, buttonW, h);

    // 进度条
    CGFloat sliderX = CGRectGetMaxX(self.previousChapter.frame) + CL_SPACE_SA_10;
    CGFloat sliderW = w - 2 * sliderX;
    self.slider.frame = CGRectMake(sliderX, 0, sliderW, h);
}


@end
